layui.define(['form','table','element'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table,element = layui.element;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/cdb/device/cache/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'deviceSn', title: '设备序列号'}
      ,{field: 'deviceTypeName', title: '设备类型'}
      ,{field: 'slotTotal', title: '卡槽数'}
      ,{field: 'stateName', title: '状态'}
      ,{field: 'iotNodeName', title: 'IOT节点'}
      ,{field: 'versionNumber', title: '版本号'}
      ,{field: 'online', title: '在线状态',templet:function (record){
          if(record.online){
              return '<span class="layui-badge layui-bg-green">在线</span>';
          }
          return '离线';
              }}
      ,{field: 'rssi', title: '信号强度'}
      ,{field: 'lastOfflineDate', title: '上次离线时间'}
      ,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });

    table.render({
        elem: '#LAY-slot-table'
        ,url: layui.setter.webRoot + '/cdb/device/slot/cache/list.data'
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'deviceSn', title: '设备序列号'}
            ,{field: 'slotNum', title: '卡槽号'}
            ,{field: 'portableBatterySn', title: '电池序列号'}
            ,{field: 'stateName', title: '卡槽状态'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-slot-table-toolbar'}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    element.on('tab(tab-info)', function (data) {
        if(data.index == 0) {
            table.reload('LAY-table')
        }else if(data.index== 1){
            table.reload('LAY-slot-table')
        }
    });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
    del:function(obj){
    	layer.confirm('确定要删除该共享充电宝主机缓存吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/cdb/device/cache/del.do',
	          data:{deviceSn:obj.data.deviceSn},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
    ,delSlot:function(obj){
          layer.confirm('确定要删除该共享充电宝卡槽缓存吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/cdb/device/slot/cache/del.do',
                  data:{deviceSn:obj.data.deviceSn,slotNum:obj.data.slotNum},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('battery_cabinet_cache_list', {})
});